{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "02004d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar, Timeline\n",
    "from pyecharts.commons.utils import JsCode\n",
    "from pyecharts.faker import Faker\n",
    "import pandas as pd\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "9ca72a92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300863.SZ 21.62 20.0037\n",
      "000661.SZ 24.54 5.4666\n",
      "688023.SH 25.9 9.9577\n",
      "688390.SH 28.17 11.8411\n",
      "300841.SZ 30.77 6.5714\n",
      "688686.SH 41.0 18.9114\n",
      "688063.SH 42.39 16.3908\n",
      "300782.SZ 44.36 7.7751\n",
      "300896.SZ 51.24 7.8227\n",
      "300750.SZ 52.99 15.0921\n",
      "688608.SH 17.29 5.1657\n",
      "000568.SZ 17.45 7.2452\n",
      "688536.SH 17.98 4.4836\n",
      "603605.SH 18.0 10.0028\n",
      "688286.SH 20.46 16.3484\n",
      "000858.SZ 21.93 7.3578\n",
      "603501.SH 23.01 10.0022\n",
      "300782.SZ 45.06 7.328\n",
      "600519.SH 62.45 3.1272\n",
      "300841.SZ 88.99 17.8333\n",
      "600763.SH 12.81 4.5155\n",
      "688063.SH 12.87 4.2881\n",
      "300760.SZ 13.78 3.1604\n",
      "688390.SH 17.5 6.5666\n",
      "300677.SZ 20.5 12.2388\n",
      "688169.SH 22.99 2.4251\n",
      "603392.SH 23.3 10.0\n",
      "000661.SZ 32.61 6.8366\n",
      "600519.SH 40.55 1.969\n",
      "300928.SZ 48.98 128.7254\n",
      "300926.SZ 20.39 189.4981\n",
      "601888.SH 23.0 7.9861\n",
      "300927.SZ 27.84 207.9164\n",
      "600809.SH 28.69 7.2633\n",
      "688390.SH 31.0 10.9155\n",
      "688063.SH 31.0 9.9042\n",
      "600519.SH 40.0 1.9048\n",
      "300751.SZ 44.0 7.1082\n",
      "688686.SH 51.1 18.6564\n",
      "688617.SH 182.56 245.1786\n",
      "003026.SZ 6.91 10.0058\n",
      "688008.SH 7.01 8.3462\n",
      "000661.SZ 7.09 1.4066\n",
      "603392.SH 7.4 3.0081\n",
      "300894.SZ 8.04 16.0511\n",
      "688027.SH 8.31 3.4397\n",
      "603087.SH 10.01 6.3358\n",
      "300677.SZ 10.38 5.6108\n",
      "603195.SH 17.65 7.8444\n",
      "688339.SH 19.08 7.2245\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'D:\\\\GitHub\\\\timeline_bar_with_graphic.html'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "df = pd.read_excel('C:/Users/zhang/Desktop/stock.xls',index_col = 'date' ,date_parser='date')\n",
    "trainDF_Ascending = df.sort_index(ascending=True) #日期升序\n",
    "trainDF_Descending = df.sort_index(ascending=False) #日期降序\n",
    "\n",
    "def date_range(start_date,end_date):\n",
    "    for n in range(int((end_date-start_date).days)):\n",
    "        yield start_date+datetime.timedelta(n)\n",
    "\n",
    "\n",
    "\n",
    "start=datetime.datetime(2021,1,4,0,0,0)\n",
    "end=datetime.datetime(2021,1,9,0,0,0)\n",
    "tl = Timeline()\n",
    "for day in date_range(start, end):\n",
    "    # print(day.strftime('%Y-%m-%d'))\n",
    "    ppt=trainDF_Ascending[day:day].groupby('code').sum().sort_values(by='rate').tail(10)\n",
    "    # print(ppt)\n",
    "    # for i in pd.date_range('20210101', '20211230',freq='YS')\n",
    "    for row in ppt.itertuples():\n",
    "           print(row.Index,getattr(row, 'rate'), getattr(row, 'price')) # 输出每一行\n",
    "    i = pd.Series(ppt.index, index=ppt.index).values\n",
    "    j = pd.Series(ppt['rate'], index=ppt.index).values\n",
    "    bar = (\n",
    "        Bar()\n",
    "        .add_xaxis(i)\n",
    "        .add_yaxis(\"商家A\", j)\n",
    "        .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(\"\".format(i)),\n",
    "            graphic_opts=[\n",
    "                opts.GraphicGroup(\n",
    "                    graphic_item=opts.GraphicItem(\n",
    "                        rotation=JsCode(\"Math.PI / 4\"),\n",
    "                        bounding=\"raw\",\n",
    "                        right=100,\n",
    "                        bottom=110,\n",
    "                        z=100,\n",
    "                    ),\n",
    "                    children=[\n",
    "                        opts.GraphicRect(\n",
    "                            graphic_item=opts.GraphicItem(\n",
    "                                left=\"center\", top=\"center\", z=100\n",
    "                            ),\n",
    "                            graphic_shape_opts=opts.GraphicShapeOpts(\n",
    "                                width=400, height=50\n",
    "                            ),\n",
    "                            graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(\n",
    "                                fill=\"rgba(0,0,0,0.3)\"\n",
    "                            ),\n",
    "                        ),\n",
    "                        opts.GraphicText(\n",
    "                            graphic_item=opts.GraphicItem(\n",
    "                                left=\"center\", top=\"center\", z=100\n",
    "                            ),\n",
    "                            graphic_textstyle_opts=opts.GraphicTextStyleOpts(\n",
    "                                text=\"某商店{}年营业额\".format(i),\n",
    "                                font=\"bold 26px Microsoft YaHei\",\n",
    "                                graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(\n",
    "                                    fill=\"#fff\"\n",
    "                                ),\n",
    "                            ),\n",
    "                        ),\n",
    "                    ],\n",
    "                )\n",
    "            ],\n",
    "        )\n",
    "    )\n",
    "    tl.add(bar, \" \")\n",
    "tl.render(\"timeline_bar_with_graphic.html\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4ed3a690",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['300863.SZ' '000661.SZ' '688023.SH' '688390.SH' '300841.SZ' '688686.SH'\n",
      " '688063.SH' '300782.SZ' '300896.SZ' '300750.SZ']\n",
      "[21.62 24.54 25.9  28.17 30.77 41.   42.39 44.36 51.24 52.99]\n",
      "['688608.SH' '000568.SZ' '688536.SH' '603605.SH' '688286.SH' '000858.SZ'\n",
      " '603501.SH' '300782.SZ' '600519.SH' '300841.SZ']\n",
      "[17.29 17.45 17.98 18.   20.46 21.93 23.01 45.06 62.45 88.99]\n",
      "['600763.SH' '688063.SH' '300760.SZ' '688390.SH' '300677.SZ' '688169.SH'\n",
      " '603392.SH' '000661.SZ' '600519.SH' '300928.SZ']\n",
      "[12.81 12.87 13.78 17.5  20.5  22.99 23.3  32.61 40.55 48.98]\n",
      "['300926.SZ' '601888.SH' '300927.SZ' '600809.SH' '688390.SH' '688063.SH'\n",
      " '600519.SH' '300751.SZ' '688686.SH' '688617.SH']\n",
      "[ 20.39  23.    27.84  28.69  31.    31.    40.    44.    51.1  182.56]\n",
      "['003026.SZ' '688008.SH' '000661.SZ' '603392.SH' '300894.SZ' '688027.SH'\n",
      " '603087.SH' '300677.SZ' '603195.SH' '688339.SH']\n",
      "[ 6.91  7.01  7.09  7.4   8.04  8.31 10.01 10.38 17.65 19.08]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "df = pd.read_excel('C:/Users/zhang/Desktop/stock.xls',index_col = 'date' ,date_parser='date')\n",
    "trainDF_Ascending = df.sort_index(ascending=True) #日期升序\n",
    "trainDF_Descending = df.sort_index(ascending=False) #日期降序\n",
    "\n",
    "def date_range(start_date,end_date):\n",
    "    for n in range(int((end_date-start_date).days)):\n",
    "        yield start_date+datetime.timedelta(n)\n",
    "\n",
    "\n",
    "\n",
    "start=datetime.datetime(2021,1,4,0,0,0)\n",
    "end=datetime.datetime(2021,1,9,0,0,0)\n",
    "tl = Timeline()\n",
    "for day in date_range(start, end):\n",
    "    # print(day.strftime('%Y-%m-%d'))\n",
    "    ppt=trainDF_Ascending[day:day].groupby('code').sum().sort_values(by='rate').tail(10)\n",
    "    # print(ppt)\n",
    "    # for i in pd.date_range('20210101', '20211230',freq='YS')\n",
    "    # for row in ppt.itertuples():\n",
    "    #        print(row.Index,getattr(row, 'rate'), getattr(row, 'price')) # 输出每一行\n",
    "    i = pd.Series(ppt.index, index=ppt.index)\n",
    "    j = pd.Series(ppt['rate'], index=ppt.index)\n",
    "    print(i.values)\n",
    "    print(j.values)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "23705b68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['300752.SZ', '688686.SH', '300761.SZ', '300910.SZ', '300082.SZ', '300762.SZ', '300863.SZ', '300890.SZ', '688568.SH', '300549.SZ']\n",
      "[16.881, 18.9114, 19.02, 19.685, 19.9616, 20.0, 20.0037, 20.0038, 20.0045, 20.0089]\n",
      "['300892.SZ', '688618.SH', '688286.SH', '300841.SZ', '300898.SZ', '300106.SZ', '300082.SZ', '300922.SZ', '688277.SH', '300344.SZ']\n",
      "[14.6234, 15.3735, 16.3484, 17.8333, 19.992, 20.0, 20.0, 20.0, 20.0106, 20.0903]\n",
      "['000633.SZ', '603603.SH', '300008.SZ', '300677.SZ', '300722.SZ', '300752.SZ', '300639.SZ', '300157.SZ', '300581.SZ', '300211.SZ']\n",
      "[10.099, 10.1075, 11.4198, 12.2388, 12.3582, 12.6897, 13.0616, 19.8953, 20.0046, 20.0608]\n",
      "['601866.SH', '300696.SZ', '300618.SZ', '688390.SH', '300274.SZ', '300767.SZ', '300925.SZ', '300116.SZ', '300001.SZ', '688686.SH']\n",
      "[10.1351, 10.2883, 10.8508, 10.9155, 11.0279, 13.856, 14.2829, 15.1832, 18.1244, 18.6564]\n",
      "['300418.SZ', '300925.SZ', '300927.SZ', '688026.SH', '300307.SZ', '300321.SZ', '300894.SZ', '300305.SZ', '300459.SZ', '300038.SZ']\n",
      "[11.715, 11.8609, 12.7577, 13.8137, 15.6863, 15.942, 16.0511, 19.9752, 20.0, 20.1201]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'D:\\\\GitHub\\\\timeline_bar_reversal.html'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_excel('C:/Users/zhang/Desktop/stock.xls',index_col = 'date' ,date_parser='date')\n",
    "trainDF_Ascending = df.loc[df['price']<21].sort_index(ascending=True) #日期升序\n",
    "trainDF_Descending = df.loc[df['price']<21].sort_index(ascending=False) #日期降序\n",
    "\n",
    "def date_range(start_date,end_date):\n",
    "    for n in range(int((end_date-start_date).days)):\n",
    "        yield start_date+datetime.timedelta(n)\n",
    "\n",
    "start=datetime.datetime(2021,1,4,0,0,0)\n",
    "end=datetime.datetime(2021,1,9,0,0,0)\n",
    "tl = Timeline()\n",
    "for day in date_range(start, end):\n",
    "    # print(day.strftime('%Y-%m-%d'))\n",
    "    ppt=trainDF_Ascending[day:day].groupby('code').sum().sort_values(by='price').tail(10)\n",
    "    # print(ppt)\n",
    "    # for i in pd.date_range('20210101', '20211230',freq='YS')\n",
    "    # for row in ppt.itertuples():\n",
    "    #        print(row.Index,getattr(row, 'rate'), getattr(row, 'price')) # 输出每一行\n",
    "    i = pd.Series(ppt.index, index=ppt.index).tolist()\n",
    "    j = pd.Series(ppt['price'], index=ppt.index).tolist()\n",
    "    print(i)\n",
    "    print(j)\n",
    "    bar = (\n",
    "        Bar()\n",
    "        .add_xaxis(i)\n",
    "        .add_yaxis(\"商家A\",  j, label_opts=opts.LabelOpts(position=\"right\"))\n",
    "        .reversal_axis()\n",
    "        .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(\"Timeline-Bar-Reversal (时间: {}-{})\".format(start,end))\n",
    "        )\n",
    "    )\n",
    "    tl.add(bar, \"{}年\".format(day))\n",
    "tl.render(\"timeline_bar_reversal.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6fcadf81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "print(type(Faker.choose()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9a936df",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
